//
//  RootViewController.m
//  早分享音乐播放器
//
//  Created by dllo on 15/12/4.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RootViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface RootViewController ()<AVAudioPlayerDelegate>

@property (nonatomic, retain) AVAudioPlayer *player;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, retain) UIProgressView *progress;
@property (nonatomic, retain) UISwitch *switcher;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSString *str = [[NSBundle mainBundle] pathForResource:@"111" ofType:@"wav"];
    
    NSURL *url = [NSURL fileURLWithPath:str];
    
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    self.player.delegate = self;
    
    // 设置单曲循环
    self.player.numberOfLoops = -1;
    // 准备播放
    [self.player prepareToPlay];
    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
    [self.view addSubview:self.progress];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progressAction) userInfo:nil repeats:YES];
    
    
    self.switcher = [[UISwitch alloc] initWithFrame:CGRectMake(50, 300, 50, 30)];
    [self.view addSubview:self.switcher];
    [_switcher release];
    [self.switcher addTarget:self action:@selector(switchAction) forControlEvents:UIControlEventValueChanged];
    
    
}

// 设置静音
- (void)switchAction
{
    self.player.volume = self.switcher.on;
}

- (void)progressAction
{
    self.progress.progress = self.player.currentTime / self.player.duration;
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    
}


- (IBAction)playAction:(id)sender {
    [self.player play];
}
- (IBAction)pauseAction:(id)sender {
    [self.player pause];
}
- (IBAction)stopAction:(id)sender {
    self.player.currentTime = 0;
    [self.player stop];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
